package org.aperture.exploit;

import java.io.File;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TableColumn;
import exploit.Hash;
import exploit.FileTree;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

//import org.eclipse.swt.*;
//import org.eclipse.swt.widgets.List;
//import org.eclipse.jface.viewers.ListViewer;
//import org.eclipse.swt.events.*;
//import org.eclipse.swt.layout.*;
//import org.eclipse.swt.widgets.Composite;
//import org.eclipse.jface.viewers.TreeViewer;
//import org.eclipse.jface.layout.TreeColumnLayout;
//import org.eclipse.swt.widgets.Label;

public class Main {

	protected Shell shell;
	private Table table;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();

		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent (Event e) {
		    	//table.redraw(0, 0, shell.getSize().x, shell.getSize().y, false);
		    	//table.redraw();
		    	table.setSize(shell.getSize().x, shell.getSize().y);
		    	//table.setBounds(shell.getBounds());
	    	}
		});
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(1300, 825);
		shell.setText("SWT Application");
		shell.setLayout(null);
		
		table = new Table(shell, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.MULTI);
		table.setHeaderVisible(true);
		table.setBounds(430, 269, 854, 498);
		table.setLinesVisible(false);
		table.setRedraw(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setMoveable(true);
		tblclmnNewColumn.setWidth(357);
		tblclmnNewColumn.setText("Filename");
		
		TableColumn tblclmnFilePath = new TableColumn(table, SWT.NONE);
		tblclmnFilePath.setMoveable(true);
		tblclmnFilePath.setWidth(357);
		tblclmnFilePath.setText("File Path");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setMoveable(true);
		tblclmnNewColumn_1.setWidth(199);
		tblclmnNewColumn_1.setText("MD5");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setMoveable(true);
		tblclmnNewColumn_2.setWidth(195);
		tblclmnNewColumn_2.setText("SHA-256");
		
		TableColumn tblclmnSha = new TableColumn(table, SWT.NONE);
		tblclmnSha.setMoveable(true);
		tblclmnSha.setWidth(729);
		tblclmnSha.setText("SHA-512");
		
		
		String path = "E:/New Folder/20110929 - E Backup/";
		String file, fileName, filePath;
		File folder = new File(path);
		File[] fileList = folder.listFiles();
		
		for (int i = 0; i < fileList.length; i++) {
			if (fileList[i].isFile()) {
				file = fileList[i].getPath();
				fileName = fileList[i].getName();
				filePath = fileList[i].getParentFile().toString();
				TableItem tableItem = new TableItem(table, SWT.NONE);
				try {
					tableItem.setText(new String[] { fileName, filePath, Hash.getHashHex(file, "MD5"), Hash.getHashHex(file, "SHA-256"), Hash.getHashHex(file, "SHA-512") });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		for (TableColumn tc : table.getColumns()) {
			tc.pack();
		}
		//table.pack();
		//shell.pack();
		
		Menu mainMenu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(mainMenu);
		
		MenuItem mainMenu_FileMenu = new MenuItem(mainMenu, SWT.NONE);
		mainMenu_FileMenu.setText("File");
		
		MenuItem mntmTools = new MenuItem(mainMenu, SWT.NONE);
		mntmTools.setText("Tools");
		
//		
//		FIX THE FINAL keyword on the Tree declaration, should be able to get rid of it.
//		
//		
//		
//		
		
		final Tree tree = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tree.setBounds(0, 0, 424, 767);
		formToolkit.adapt(tree);
		formToolkit.paintBordersFor(tree);
		
		TreeColumn trclmnNewColumn = new TreeColumn(tree, SWT.NONE);
		trclmnNewColumn.setWidth(tree.getSize().x);
		trclmnNewColumn.setText("New Column");
		tree.setSortColumn(trclmnNewColumn);
		tree.setSortDirection(SWT.DOWN);
		
		tree.addListener(SWT.Selection, new Listener() {
		      public void handleEvent(Event e) {
		        String string = "";
		        final TreeItem[] selection = tree.getSelection();
//		        for (int i = 0; i < selection.length; i++)
//		          string += selection[i] + " ";
		        //System.out.println("Selection={" + string + "}");
		        
		        //string = string + "/" + getParent(selection[0]).getText();
		        
//		        if (selection[0].getParentItem() != null) {
//		        	
//		        	string = selection[0].getText() + "/" + string;
//		        	
//		        	System.out.println("String: " + string);
//		        }
		        
		        //TreePath selectedPath = tree.g
		        
		        //string = "/" + getParent(selection[0], string).getText();
		        //selection[0].getdata
		        System.out.println("String: " + getParent(selection[0], string).getText());
		        
		        
		        
		        
//		        string = string + "/" + selection[0].getText();
//	        	System.out.println("String: " + string);
		      }
			});
		
		
		File[] roots = File.listRoots();
		for (int i = 0; i < roots.length; i++) {
			System.out.println(i);
			System.out.println(roots[i].getAbsolutePath());
//			if (roots[i].isDirectory()) {
//				TreeItem item = new TreeItem(tree, SWT.NONE);
//				item.setImage(SWTResourceManager.getImage(Main.class, "/resources/icons/oxygen-icons-by-oxygen/png/16x16/devices/drive_harddisk.png"));
//				item.setText(roots[i].getPath());
//				FileTree.getDirectoryContents(roots[i].getAbsolutePath(), item);
//			}
			if (roots[i].isDirectory()) {
				TreeItem item = new TreeItem(tree, SWT.NONE);
				item.setImage(SWTResourceManager.getImage(Main.class, "/resources/icons/oxygen-icons-by-oxygen/png/16x16/devices/drive_harddisk.png"));
				item.setText("D:/Incoming");
				FileTree.getFiles("D:/Incoming", item);
				
			}

			System.out.println(roots[i].getPath());
		}
		


	}
	
//	public TreeItem getParent(TreeItem child) {
//		try {
//			if (child.getParentItem() != null) {
//				getParent(child);
//			}
//			
//		} catch (Exception e) {
//			
//		}
//		return child.getParentItem();
//	}
	
	public TreeItem getParent(TreeItem child, String string) {
		if (child.getParentItem() == null) {
			return child;
		}
		getParent(child.getParentItem(), string);
		string = child.getParentItem().getText() + "/";
		return child.getParentItem();
	}
	
	
}
